<?php
namespace app\admin\logic;
use think\Model;
use app\admin\model\Article as ArticleModel;
class Article extends ArticleModel
{
	protected $result;

	protected function initialize()
	{
		//先继承父级初始参数
		parent::initialize();

		//结果集
		$this->result ['bool']			= false;	//成功 true  失败 false
		$this->result ['code']  		= 0;		//消息代号
		$this->result ['message']  		= '';		//消息
		$this->result ['data']  		= array();	//数据结果
	}


	public function dataList($map='', $order='ID DESC', $limit=10)
	{
		return $this->lists($map, $order, $limit);
	}

	public function dataFind($id)
	{
		return $this->one($id);
	}
	//添加文章数据逻辑
	public function dataAdd($data)
	{

		if( empty($data) ) {
			$this->result['code'] = -10005;
			return $this->result;
		}

		$res = $this->updates($data);
		
		if( $res >= 1 ) {
			$this->result['bool'] = true;
			$this->result['data']['id'] = $res;
		}
		$this->result ['code'] = $res;


		return $this->result;
	}

	public function dataUpdate($data, $id)
	{
		if( empty($data) ) {
			$this->result ['code'] = -10005;
			return $this->result;
		}

		if( empty($id) ) {
			$this->result ['code'] = 'param';
			return $this->result;
		}

		$res = $this->updates($data, $id);

		if( $res >= 1 ) {
			$this->result ['bool'] = true;
		} else {
			$this->result ['code'] = $res;
		}
		return $this->result;
	}
	/**
	 * 软删除 删除一条文章数据
	 * @param  int $id 文章ID
	 * @return [type]     [description]
	 */
	public function dataDel($id)
	{
		$res = $this->del($id);
		if( $res == 1 ) {
			$this->result ['bool'] = true;
		} else {
			$this->result ['code'] 	= $res;
		}
		return $this->result;
	}
	/**
	 * 删除多条文章数据 
	 * @param  array $ids  ,拼接起来的文章ID 
	 * @return array       
	 */
	public function dataDels($ids)
	{	
		//变量定义
		$code 		= '';
		$message 	= '';
		//参数处理
		$ids = explode(',', rtrim($ids, ','));
		$res = $this->dels($ids);
		if($res == 1) {
			$this->result ['bool'] = true;
		} else {
			foreach ($res as $key => $value) {
				$code 		= $value['code'];
				$message 	.= $value['id'] . ' | ';
			}
			$this->result ['code'] 		= $code;
			$this->result ['message'] 	= ' ID ' . rtrim($message, ' |');
		}
		return $this->result;
	}
}